﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DotaHelp.Models;
using System.IO;
using System.Web.UI;
using System.Drawing;
using DotaHelp.Helpers;
using Sandbox.AddressBook.Web.Client.Application.Mvc;

namespace DotaHelp.Controllers
{
   
    public class ItemsController : Controller
    {
        DotaModel model = new DotaModel();

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            model.Dispose();
            
        }

        [Sitemap()]
        public ActionResult List()
        {
			return View(model.Items.Include("Recipes").Where(i => i.Id != -1).OrderBy(i => i.Price).ToArray());
        }

		
		public ActionResult SmallList()
		{
			return PartialView(model.Items.Include("Recipes").Where(i => i.Id != -1).OrderBy(i => i.Price).ToArray());
		}

        [Sitemap(typeof(Item))]
        public ActionResult Details(int id)
        {
            var item = model.Items.Include("Recipes1").Include("Recipes1.ForItem").First(i => i.Id == id);
            ViewBag.Root = RecipeDrawItem.Load(model, id);
			return View(item);
        }

        public ActionResult Title(int id)
        {
            var item = model.Items.Include("Recipes1").Include("Recipes1.ForItem").First(i => i.Id == id);
            ViewData["Color"] = Color.Gold;
            ViewData["Root"] = RecipeDrawItem.Load(model, id);
			return PartialView(item);
        }

        [ChildActionOnly]
        public ActionResult One(int id)
        {
            var item = model.Items.FirstOrDefault(i => i.Id == id);
            return item != null ? (ActionResult)PartialView(item) : new ContentResult() { Content = "i" + id };            
        }

        [IfModified("RecipeImage3", "itemId", "lineColor")]
        [OutputCache(Duration=600, VaryByParam="itemId;lineColor", Location=OutputCacheLocation.ServerAndClient)]
		[ValidateInput(false)]
        public ActionResult RecipeImage(int itemId, Color lineColor)
        {
            var item = RecipeDrawItem.Load(model, itemId);

            using (var img = item.Draw(model, lineColor))
            using (var stream = new MemoryStream())
            {
                img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                return new FileContentResult(stream.GetBuffer(), "image/png");
            }
        }

        [Sitemap]
        public ActionResult Shops()
        {
            return View();
        }

        //[IfModified("ShopDetail", "name")]
        public ActionResult ShopDetail(string name)
        {            
            return PartialView(model.Shops.Include("ItemShops").Include("ItemShops.Item").First(s=>s.Name == name));
        }

        [Authorize(Roles="Admin")]
        public ActionResult EditShops()
        {
            ViewBag.Items = model.Items.Include("ItemShops").ToArray();
            ViewBag.Shops = model.Shops.ToArray();

            return View();
        }

        [Authorize(Roles="Admin")]
        public void ChangeShop(FormCollection collection)
        {
            var itemId = int.Parse(collection["itemId"]);
            var item = model.Items.Include("ItemShops").First(i => i.Id == itemId);
            foreach (var s in item.ItemShops.ToArray())
            {
                model.DeleteObject(s);
                item.ItemShops.Remove(s);   
            }
           

            foreach (var shop in model.Shops)
            {
                int position;
                if (int.TryParse(collection[shop.Name], out position))
                {
                    item.ItemShops.Add(new ItemShop() { Position = position - 1, Shop = shop });
                }
            }

            model.SaveChanges();
        }
    }
}
